library(MASS)
library(vegan)
library(foreign)
library(rworldmap)
library(RColorBrewer)
library(classInt)
library(dplyr)





gamma2017 <- read.dta(file="gamma2017.dta")
# country labels to the map
sPDF <- joinCountryData2Map(gamma2017, joinCode = "ISO_N3", nameJoinColumn = "iso3n2", verbose=TRUE)




 
                  
 #Drawing the map

out.file <- paste("polex_map.pdf",sep="")
pdf(file=out.file, height=4, width=7)
par(mar=c(4,1,2,1))

#Setting up class intervals for continuous variable
classInt <- classIntervals(sPDF$gamma, n=8, style="fixed",  fixedBreaks=c(-2.2, -2, -0.5, 0.1, 0.4, 0.6, 0.8, 1.4, 1.7))
catMethod=classInt$brks

#Selecting diverging palette
colourPalette <- brewer.pal(9,"Greys")
#colourPalette <- brewer.pal(9,"PuOr")

#Drawing the map

mapParams <- mapCountryData(sPDF,nameColumnToPlot="gamma", 
                            #catMethod="fixed", 
                            catMethod=catMethod,
                            mapTitle="Leaders in 2017",
                            colourPalette=colourPalette,
                            #oceanCol="lightblue",
                            missingCountryCol="white", 
                            addLegend="FALSE"
                            #borderCol="black"
)


#adding legend
do.call( addMapLegend, c( mapParams, legendLabels="all", 
                          labelFontSize=0.5,legendShrink=0.8,
                           legendWidth=0.5, legendIntervals="page"))
dev.off() 
             